package com.aw.guiguzi.module.manager._login

import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.aw.baselib.base.AwMvpBindingActivity
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwRecyclerViewUtil
import com.aw.baselib.util.AwSoftKeyboardUtil
import com.aw.baselib.widgets.MySmartRefreshLayout
import com.aw.baselib.widgets.SearchEditText
import com.aw.baselib.widgets.SearchEditText.OnSearchClickListener
import com.aw.guiguzi.R
import com.aw.guiguzi.bean.LoginRecordItemBean
import com.aw.guiguzi.constants.MyConstant
import com.aw.guiguzi.databinding.ActivityManagerLoginListBinding
import com.aw.guiguzi.module.manager._login.adapter.LoginRecordListAdapter
import com.aw.guiguzi.module.manager._login.presenters.LoginReordListPresent
import com.aw.guiguzi.module.manager._login.views.LoginRecordListView
import com.chad.library.adapter.base.BaseQuickAdapter.RequestLoadMoreListener
import com.scwang.smartrefresh.layout.api.RefreshLayout

class LoginRecordListActivity :
    AwMvpBindingActivity<LoginReordListPresent?, ActivityManagerLoginListBinding?>(),
    LoginRecordListView.View, RequestLoadMoreListener {
    private var mAdapter: LoginRecordListAdapter? = null
    private var index = 1
    private var total = 0
    private var mList: MutableList<LoginRecordItemBean> = ArrayList()
    private var paramSearchUser = ""
    private var isLimit7Days = true
    override fun createPresenter(): LoginReordListPresent {
        return LoginReordListPresent(this)
    }

    override fun getViewBinding(): ActivityManagerLoginListBinding {
        return ActivityManagerLoginListBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        setToolbarWithBackImgAndRightView("登录列表", "当前范围(7天)") {
            isLimit7Days = !isLimit7Days
            mToolbar.setRightText(if (isLimit7Days) "当前范围(7天)" else "当前范围(所有)")
            getData(true)
        }
    }

    override fun initData() {
        super.initData()
        mAdapter = LoginRecordListAdapter()
        AwRecyclerViewUtil.setRecyclerViewLinearlayout(mActivity, binding?.rcvData, mAdapter, false)
        getData(true)
    }

    override fun initListener() {
        super.initListener()
        binding?.sflLayout?.setOnRefreshListener { refreshLayout: RefreshLayout? -> getData(true) }
        mAdapter!!.setOnLoadMoreListener(this, binding?.rcvData)
        binding?.etSearch?.setOnSearchClickListener(object : OnSearchClickListener {
            override fun onSearchClick(view: View) {
                paramSearchUser = getText(binding?.etSearch)
                getData(true)
                AwSoftKeyboardUtil.checkAndClose(mActivity, binding?.etSearch)
            }

            override fun onClearSearch() {
                paramSearchUser = ""
                getData(true)
            }
        })
    }

    private fun getData(needResetIndex: Boolean) {
        if (needResetIndex) {
            index = 1
            mList = ArrayList()
            mAdapter!!.notifyDataSetChanged()
        }
        mPresenter!!.getLoginList(index, paramSearchUser, isLimit7Days)
    }

    override fun onLoadMoreRequested() {
        if (mList.size < total) {
            index++
            getData(false)
        } else {
            if (index == 1 && mList.size < MyConstant.COMMON_LIST_PAGE_LIMIT_LARGE) {
                mAdapter!!.loadMoreEnd(true)
            } else {
                mAdapter!!.loadMoreEnd()
            }
        }
    }

    override fun getLoginListSuccess(total: Int, list: List<LoginRecordItemBean>) {
        this.total = total
        binding?.sflLayout?.finishRefresh()
        if (AwDataUtil.isEmpty(list)) {
            AwRecyclerViewUtil.resetView(mActivity, mAdapter, binding?.rcvData, index == 1)
            return
        }
        mList.addAll(list)
        mAdapter!!.addAllData(mList)
        mAdapter!!.loadMoreComplete()
        mAdapter!!.disableLoadMoreIfNotFullPage(binding?.rcvData)
    }

    override fun getLoginListFail(code: Int, msg: String?) {
        binding?.sflLayout?.finishRefresh()
        AwRecyclerViewUtil.resetView(mActivity, mAdapter, binding?.rcvData, index == 1)
    }
}